package min.chapter.three;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class LifecycleAcquisition {

	public static final String CONFIG_FILE_PATH = "min/chapter/three/Lifecycle.xml";
	public static void main(String[] args) {
	useApplicationFactory();
	System.out.println("\n\n");
	useBeanFactory(false);
	System.out.println("\n\n");
	useBeanFactory(true);
	System.out.println("\n\n");
	}
	private static void useApplicationFactory() {
	System.out.println("--- useApplicationFactory");
	String[] configLocations = new String[] { CONFIG_FILE_PATH };
	AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
	context.registerShutdownHook();
	}
	private static void useBeanFactory(boolean addBeanPostProcessor) {
	System.out.println("--- useBeanFactory" + (addBeanPostProcessor ? ": Use BeanPostProcessor" : ""));
	Resource resource = new ClassPathResource(CONFIG_FILE_PATH);
	XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
	if (addBeanPostProcessor) {
	beanFactory.addBeanPostProcessor(new LifeCycleBeanPostProcessor());
	}
	String beanName = "allILifeCycleInterfacesImpl";
	LifeCycleAllImplement bean = beanFactory.getBean(beanName, LifeCycleAllImplement.class);
	System.out.println(bean);
	beanFactory.removeBeanDefinition(beanName);
	}
	}
